package com.agent.common.config;

import com.agent.common.interceptor.AdminAuthInterceptor;
import com.agent.common.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC配置
 * @author Jeff_Wan
 * @description 配置Web MVC相关设置，主要配置拦截器
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;
    private final AdminAuthInterceptor adminAuthInterceptor;
    private final SecurityProperties securityProperties;

    public WebConfig(AuthInterceptor authInterceptor, AdminAuthInterceptor adminAuthInterceptor, SecurityProperties securityProperties) {
        this.authInterceptor = authInterceptor;
        this.adminAuthInterceptor = adminAuthInterceptor;
        this.securityProperties = securityProperties;
    }

    /**
     * 添加拦截器配置
     * @param registry 拦截器注册器
     * @description 配置认证拦截器并设置拦截/排除路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(securityProperties.getExcludePaths());

        registry.addInterceptor(adminAuthInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns(securityProperties.getAdminExcludePaths());
    }
}